package com.wxapp.custom.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.custom.api.pojo.entity.CustomVipDO;
import com.wxapp.custom.api.pojo.vm.customVip.PVipDetailVm;
import com.wxapp.custom.api.pojo.vo.customVip.QVipEditVo;
import com.wxapp.custom.service.mapper.VipMapper;
import com.wxapp.custom.service.service.VipService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author lgl
 * @Description vip 管理
 * @Datetime 2022/7/4 14:54
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class VipServiceImpl extends IBaseService implements VipService {
   private final VipMapper vipMapper;
    @Override
    public ResultVm<PVipDetailVm> getVip(UserVo userVo) {
        LambdaQueryWrapper<CustomVipDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomVipDO::getCustomId, userVo.getCustomId()).eq(CustomVipDO::getStatus, true);
        var wxappUserVipDO = vipMapper.selectFirst(wrapper);
        if (CheckUtil.isNullObject(wxappUserVipDO)) {
            return success(new PVipDetailVm());
        }
        return success(CopyUtil.copyObjectNew(wxappUserVipDO, PVipDetailVm::new, null));
    }

    @Override
    public ResultVm<String> edit(QVipEditVo qEditVm, UserVo userVo) {
        LambdaQueryWrapper<CustomVipDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomVipDO::getStatus, true).eq(CustomVipDO::getCustomId, userVo.getCustomId());
        var userVipDO = vipMapper.selectFirst(wrapper);
        if (CheckUtil.isNotNullObject(userVipDO)) {
            CopyUtil.copyObject(qEditVm, userVipDO, null);
            vipMapper.updateById(userVipDO);
            return success("更新用户vip成功");
        }
        var userVipNew = CopyUtil.copyObjectNew(qEditVm, CustomVipDO::new, (r, s) -> {
            s.setCreateBy(userVo.getId());
            return null;
        });
        vipMapper.insert(userVipNew);
        return success("更新用户vip成功");
    }
}
